

function image_rotator  (id,interval,fadetime) {
    var $_ir = this;
    
    $.ajax({
        async: false,
        url: "/modules/general/jquery.timers.js",
        dataType: "script"
    });
    $_ir.id = (id ? id : "rotator");
    $_ir.interval = (interval ? interval : 6000);
    $_ir.fadetime = (fadetime ? fadetime : 1000);
    $_ir.n = 1;
    $_ir.goToN = -1;
    $_ir.t = "";
    
    $_ir.recursive_key = 0;
  
  
 
    
    $_ir.theRotator= function () {
        
        
        var n = $("#" + $_ir.id + " > ul > li").size();
        
        
        if (n>1) {
            for (var i=1; i<=n; i++) $_ir.rotate();
            
            setTimeout (function () {$_ir.start_rotate()}, $_ir.interval);
        } else {
           $("#" + $_ir.id).show();
           $("#" + $_ir.id + " > ul > li img").fadeIn($_ir.fadetime);
           $("#" + $_ir.id + " > ul > li div").fadeIn($_ir.fadetime);
        }
       
    };  
    
    $_ir.start_rotate = function () {
        $_ir.recursive_key = 1;
        $_ir.rotate($_ir.recursive_key);
    }
    
    $_ir.rotate = function (recursive) {
        //if (!$_ir.running) {
           recursive = (recursive ? recursive : false);
           if ((recursive)&&(recursive != $_ir.recursive_key)) return false;
           $_ir.recursive_key++;
           //$("#" + $_ir.id + " li img").stop();
           $("#" + $_ir.id + " > ul > li:last img").fadeOut($_ir.fadetime)
           $("#" + $_ir.id + " > ul > li:last div").fadeOut($_ir.fadetime)
           var n = $("#" + $_ir.id + " > ul > li").size()-1
           $("#" + $_ir.id + " > ul > li:nth-child(" + n +   ") img").fadeIn($_ir.fadetime, function (){$("#" + $_ir.id).show()})
           $("#" + $_ir.id + " > ul > li:nth-child(" + n +   ") div").fadeIn($_ir.fadetime, function (){$("#" + $_ir.id).show()})
           
           var $me = $("#" + $_ir.id + " > ul > li:last");
           $me.parent().prepend($me)
           if (recursive) {
               var currentTime = new Date()
               console.log(currentTime.getMinutes() + ":" + currentTime.getSeconds() + " " + $_ir.recursive_key )
               
               //$(this).oneTime($_ir.interval, function () {$_ir.rotate($_ir.recursive_key)})
               $_ir.t = setTimeout(function () {$_ir.rotate($_ir.recursive_key)}, $_ir.interval);
           }
           return true;
          // $_ir.running = false;
       // }
    };
    
    $_ir.goTo = function (id) {
        $_ir.goToN = id+1;
        clearTimeout($_ir.t)
        $_ir.rotate();
    };
    
    $_ir.initialize = function () {
            $_ir.n = $("div#" + $_ir.id + " ul li").size();
            $("div#" + $_ir.id + " > ul").css ({
                padding: 0,
                margin: 0
            });
            $("div#" + $_ir.id + " > ul > li").css ({
                "list-style": "none",
                position:"absolute",
                top:"0px"
            });
            $("div#" + $_ir.id + " > ul > li > img").css ({
                position:"absolute",
                top:"0px"
            });
            $("div#" + $_ir.id + " > ul > li > div").css ({
                position:"absolute",
                top:"0px"
            });
            $("div#" + $_ir.id + " > ul > li").css ({
            "background-color":"white"
            });
            
            $("div#" + $_ir.id + " > ul > li > div ").css ({
                "background-color":"white"
            });
  
            $_ir.theRotator();
            
    }
    
    
    
    $_ir.load = function () {
    var productElement = document.getElementById($_ir.id);
    if (productElement != null)  $_ir.initialize(); 
    else var t = setTimeout ($_ir.load,100);
    }
    
   $_ir.load();
}
